<?php

namespace Admin\Controller;



class JobController extends AdminController {



	#列表

	function index() {



		$key  = I('get.key');

		$cid = I('get.cid');



		if($cid!=''){

			$map['trainID'] = array('like', '%-'.$cid.'-%');

		}



		if($key!=''){

			$map['position'] = array('like', '%'.$key.'%');

		}



		$obj = M('Job');



		$count = $obj->where($map)->count();



		import("Common.ORG.Page");

		$page = new \Page($count, 20);



		$show = $page->show();



		$list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();

		foreach ($list as $key => $value) {

			$value['trainItem'] = trainStrtoArr($value['trainID']);

            $where['id'] = array('in',$value['trainItem']);

            $list[$key]['trainItem'] = M('TrainCate')->field('name')->where($where)->select();

		}

		$this->assign('list', $list);



		$this->assign('page', $show);



		$cate = M('TrainCate')->getField('id,name');

		$this->assign('cate', $cate);

		$this->assign('cid', $cid);

		$this->assign('keys', $key);



		$this->display();



	}



	#添加

	function Add() {

		$cate = M('TrainCate')->field("id,name")->select();

		$this->assign('train', $cate);

		$this->display();

	}



	#保存

	function Save() {

		$obj = D('Job');

		if ($data = $obj->create()) {

			$trainItem = I('trainItem');

	        if ($trainItem!='') {

	            $data['trainID'] = '-'.implode('-', $trainItem).'-';

	        }

			if ($obj->add($data)) {

				$state = 'SUCCESS';

	            $message = L('_action_success');

	            $url = U('Job/index');

			} else {

				$state = L('_action_fail');

			}

		} else {

			$state = $obj->getError();

		}

		echo $this->echo_json_str($state,$message,$url);

	}



	#编辑

	function Edit() {



		$id = (int) $_GET['id'];



		if (!isset ($id)) {

			$this->error('Parameter error');

		}



		$obj = M('Job');



		$list = $obj->where("id=$id")->find();



		if (!$list) {

			$this->error('Information does not exist');

		} else {



			$list['trainItem'] = trainStrtoArr($list['trainID']);

			$cate = M('TrainCate')->field("id,name")->select();

			$this->assign('train', $cate);



			$this->assign('list', $list);

			$this->display();

		}



	}



	#更新

	function Update() {



		$obj = D('Job');



		if ($data = $obj->create()) {

			$trainItem = I('trainItem');

	        if ($trainItem!='') {

	            $data['trainID'] = '-'.implode('-', $trainItem).'-';

	        }

			if ($obj->save($data)) {

				$state = 'SUCCESS';

	            $message = L('_action_success');

	            $url = U('Job/index');

			} else {

				$state = L('_action_fail');

			}

		} else {

			$state = $obj->getError();

		}

		echo $this->echo_json_str($state,$message,$url);



	}



	#删除

	function Del() {



		$id=I('post.selectedids');



		if($id==''){

			$state = L("_no_selected");

		}else{

			foreach ($id as $v) {

				$obj = M('Job');

				$where['id'] = $v;

				$obj->where($where)->delete();

			}

			$state = 'SUCCESS';

	        $message = L('_action_success');

	        $url = U('Job/index');

		}

		echo $this->echo_json_str($state,$message,$url);

	}



}

?>